package com.d_project.qrcode.view {
	
	/**
	 * @author Kazuhiko Arase 
	 * http://www.d-project.com/
	 * http://www.d-project.com/qrcode/index.html
	 * */
    import com.d_project.qrcode.model.ErrorCorrectLevel;
    import com.d_project.qrcode.model.QRCode;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
  
    public class QRCode extends Sprite {
        
        public var  padding : int = 50;
    	/**
    	 * 復元能力 7%.
    	 * <br>vodafoneで使用不可?
    	 */
    	public static const ERROR_CORRECTION_L : int = 1;
    	/**
    	 * 復元能力 15%.
    	 */
    	public static const ERROR_CORRECTION_M : int = 0;
    	/**
    	 * 復元能力 25%.
    	 */
    	public static const ERROR_CORRECTION_Q : int = 3;
    	/**
    	 * 復元能力 30%.
    	 */
    	public static const ERROR_CORRECTION_H : int = 2;
    	
        private var qr 				: com.d_project.qrcode.model.QRCode;
        private var _text 			: String ='';
		private var unscaledWidth 	: int;
		private var unscaledHeight 	: int;
//		private var g 				: Graphics = graphics;
		private var bitmap			: Bitmap;
		private var canvas			: BitmapData;
		private var rect			: Rectangle;
		
        public var errorCorrectLevel : int = ERROR_CORRECTION_H;
        
        public function QRCode(dim:int=200) {

            unscaledWidth  = dim;
            unscaledHeight = dim;
            qr = null;
            canvas = new BitmapData(dim,dim,false,0xFFFFFF);
            
            bitmap = new Bitmap(canvas);
            addChild(bitmap);
            rect = new Rectangle(0,0,dim,dim);
        }
        
        public function get text() : String {
            return _text;
        }

        public function set text(value : String) : void {
            _text = value;
            qr = null;
            render();
        }
        
        private function clear():void{
//        	g.clear();
//          g.beginFill(0xffffff);
//          g.drawRect(0, 0, unscaledWidth, unscaledHeight);
//          g.endFill();
            rect.x = rect.y = 0;
            rect.width 	= unscaledWidth;
            rect.height = unscaledHeight;
            canvas.fillRect(rect,0xFFFFFF);
        }
        
        private function render() : void {
        	clear();
        	
            var size 	: int = Math.min(unscaledWidth, unscaledHeight) - padding * 2;
            var xOffset : int = (unscaledWidth - size) / 2;
            var yOffset : int = (unscaledHeight - size) / 2;
            if (qr == null) {
                qr = com.d_project.qrcode.model.QRCode.getMinimumQRCode(text, ErrorCorrectLevel[errorCorrectLevel]);
            }
            var rows:int = qr.getModuleCount();
            var cols:int = qr.getModuleCount();
            var d: int = size / rows;
            var x:int;
            var y:int;
            var c:int;
            
            for (var row : int = 0; row < rows; row++) {
                for (var col : int = 0; col < cols; col++) {
                	x = d * col + xOffset;
                	y = d * row + yOffset;
                	c = (qr.isDark(row, col)? 0 : 0xffffff);
//                    g.beginFill( col );
//                    g.drawRect(x, y, cs, cs);
//                    g.endFill();
                    rect.x = x;
                    rect.y = y;
                    rect.width = d;
                    rect.height = d;
                    canvas.fillRect(rect,c);
                }
            }
        }
    }
}

